//
//  BFShOrderHomeViewController.m
//  BeautyFood
//
//  Created by apple on 2020/5/5.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THBabyMineOrderViewController.h"
#import "THBabyOrderTableViewCell.h"
#import "THBabyOrderDetailViewController.h"
#import "THBabyFinshViewController.h"
@interface THBabyMineOrderViewController ()
@property(nonatomic, strong) STTools_SegementAutoView                     *auto_view;/**<  */
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**<  */

@end

@implementation THBabyMineOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的订单";
    [self stoneobs_ui_configSubView];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.didLoad) {

    }
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    __weak typeof(self) weakSelf =  self;
    STTools_SegementAutoView * auto_view = [[STTools_SegementAutoView alloc] initWithFrame:CGRectMake(0, TMUtils.stoneobs_fun_get_navgationBarBootom, STONEOBS_UI_UISCREEN_WIDTH, 45) andTitle:@[@"待付款",@"待服务",@"已完成"] handle:^(STTools_SegementAutoView *sender, UIButton *currentSelctedButton) {
        weakSelf.dataSouce = NSMutableArray.new;
        weakSelf.tableView.mj_footer.hidden = YES;
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    auto_view.butTitleColor = STONEOBS_UI_FIRST_TEXT_COLOR;
    auto_view.butTitleSelectedColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    auto_view.lineSelectedColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    auto_view.lineColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    auto_view.lineColor = UIColor.clearColor;
    auto_view.autoMoveWithClic = YES;
    [self.view addSubview:auto_view];
    self.auto_view = auto_view;
    
    self.tableView.frame = CGRectMake(0, self.auto_view.bottom, STONEOBS_UI_UISCREEN_WIDTH, TMUtils.stoneobs_fun_get_tabBarTop+49 - self.auto_view.bottom);
    self.tableView.contentInset = UIEdgeInsetsZero;

    [self.tableView registerClass:THBabyOrderTableViewCell.class forCellReuseIdentifier:@"BFShOrderTableViewCell"];


    self.tableView.st_noreslutView = [[STTools_NoresultView alloc] initWithFrame:CGRectMake(0, self.tableView.tableHeaderView.height + 30, STONEOBS_UI_UISCREEN_WIDTH, 400) title:@"暂无数据" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf stoneobs_send_list_request];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return THBabyOrderTableViewCell.stoneobs_ui_cell_height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    THBabyOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[THBabyOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    THBabyOrderModel * model = self.dataSouce[indexPath.section];
    cell.model = model;
    cell.statusLable.text = @[@"代付款",@"待服务",@"已完成"][self.auto_view.cureentIndex];
    __weak typeof(self) weakSelf =  self;
    cell.payButton.hidden = self.auto_view.cureentIndex;
    if (self.auto_view.cureentIndex == 1) {
        if (model.is_sign_for.intValue == 1) {
            cell.evaButton.hidden = NO;
        }else{
            cell.evaButton.hidden = YES;
        }
        
    }else{
        cell.evaButton.hidden = YES;
    }
    [cell.payButton setClicAction:^(UIButton *sender) {
        
        THZhiFuViewController * vc = THZhiFuViewController.new;
        vc.pay_money = model.price;
        vc.order_id = model.o_id;
        vc.order_type = @"nursery";
        [weakSelf pushViewController:vc animated:1];
        
    }];
    [cell.evaButton setClicAction:^(UIButton *sender) {
        
        THBabyFinshViewController * vc = THBabyFinshViewController.new;
        vc.model = model;
        [weakSelf pushViewController:vc animated:1];
        
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    THBabyOrderModel * model = self.dataSouce[indexPath.section];
    THBabyOrderDetailViewController * vc = THBabyOrderDetailViewController.new;
    vc.easyModel = model;
    [self pushViewController:vc animated:1];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --stoneobs_net netWork Method
- (void)configRefreshFooter{
    __weak typeof(self) weakSelf =  self;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf stoneobs_send_list_request];
    }];
}
- (void)stoneobs_send_list_request{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (self.auto_view.cureentIndex == 0 ) {
        [stoneobs_net_param_dic setObject:@(0) forKey:@"status"];
    }
    if (self.auto_view.cureentIndex == 1 ) {
        [stoneobs_net_param_dic setObject:@(1) forKey:@"status"];
    }
    if (self.auto_view.cureentIndex == 2 ) {
        [stoneobs_net_param_dic setObject:@(2) forKey:@"status"];
    }
    [stoneobs_net_param_dic setObject:@(1) forKey:@"type"];
    [stoneobs_net_param_dic setObject:@(self.page) forKey:@"page"];
    //http://bf.xingzhuosong.com//v1/goods/goodsList?name=%E8%92%9C%E8%8B%97&page=1
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/nursery/orders"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        CGFloat total = [[responseObject valueForKey:@"data"][@"total"] floatValue];
        CGFloat perPage = [[responseObject valueForKey:@"data"][@"perPage"] floatValue];;//每一页数据
        total = ceil(total/perPage);//总共多少页
        
        NSArray * array = [responseObject valueForKey:@"data"][@"items"];
        NSArray * objArry = [THBabyOrderModel mj_objectArrayWithKeyValuesArray:array];
        if (self.page == 1) {
            self.dataSouce = NSMutableArray.new;
            [self.tableView.mj_header endRefreshing];
            if (total > self.page) {
                [self configRefreshFooter];
            }
        }else{
            if (total > self.page) {
                [self.tableView.mj_footer endRefreshing];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.dataSouce addObjectsFromArray:objArry];
        [self.tableView reloadData];
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        if (self.page > 1) {
            self.page --;
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
    
}
@end
